home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / lib / stdio / fclose.c < prev    next >
C/C++ Source or Header  |  1997-09-09  |  1KB  |  65 lines

  1.  
  2. /*
  3.  *  FCLOSE.C
  4.  *
  5.  *    (c)Copyright 1992-1997 Obvious Implementations Corp.  Redistribution and
  6.  *    use is allowed under the terms of the DICE-LICENSE FILE,
  7.  *    DICE-LICENSE.TXT.
  8.  */
  9.  
  10. #include <clib/dos_protos.h>
  11. #include <fcntl.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <lib/misc.h>
  15.  
  16. int
  17. fclose(fi)
  18. FILE *fi;
  19. {
  20.     int error = EOF;
  21.  
  22.     if (fi && (fi->sd_Flags & __SIF_OPEN)) {
  23.     error = __fclose(fi);
  24.     if (fi->sd_Name)
  25.         free(fi->sd_Name);
  26.     if ((fi->sd_Flags & __SIF_NOFREE) == 0) {
  27.         *fi->sd_Prev = fi->sd_Next;
  28.         if (fi->sd_Next)
  29.         fi->sd_Next->sd_Prev = fi->sd_Prev;
  30.         free(fi);
  31.     }
  32.     }
  33.     return(error);
  34. }
  35.  
  36. int
  37. __fclose(fi)
  38. FILE *fi;
  39. {
  40.     int error;
  41.     int n = fi->sd_BufSiz - fi->sd_WLeft;
  42.  
  43.     if (fi->sd_WLeft >= 0 && fi->sd_WBuf && n) {
  44.     if (write(fi->sd_Fd, fi->sd_WBuf, n) != n)
  45.         fi->sd_Error = EOF;
  46.     }
  47.     error = fi->sd_Error;
  48.     if (fi->sd_Flags & __SIF_MYBUF) {
  49.     if (fi->sd_RBuf) {
  50.         free(fi->sd_RBuf);
  51.         fi->sd_RBuf = NULL;
  52.     }
  53.     if (fi->sd_WBuf) {
  54.         free(fi->sd_WBuf);
  55.         fi->sd_WBuf = NULL;
  56.     }
  57.     }
  58.     close(fi->sd_Fd);
  59.     if (fi->sd_Flags & __SIF_REMOVE)
  60.     DeleteFile(fi->sd_Name);
  61.     fi->sd_Flags &= ~(__SIF_OPEN|__SIF_READ|__SIF_WRITE);
  62.     return(error);
  63. }
  64.  
  65.